package com.jiaoyou.baojian.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.activity.LoginRegActivity;
import com.jiaoyou.baojian.activity.UserInfoActivity;
import com.jiaoyou.baojian.adapter.ShengyouAdapter;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.entity.response.ShengyouListResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.http.response.BaseResponse;
import com.jiaoyou.baojian.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;

public class ShengyouListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    //SHAO_FU/REN_QI/SHU_NV/ YU_JIE/LUO_LI/OL
    private static final String TYPE = "SHAO_FU";
    Unbinder unbinder;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private String type;
    private ShengyouAdapter shengyouAdapter;

    private int page = 1;
    private int pageSize = 12;

    public ShengyouListFragment() {
    }

    public static ShengyouListFragment newInstance(String type) {
        ShengyouListFragment fragment = new ShengyouListFragment();
        Bundle args = new Bundle();
        args.putString(TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        recyclerview.setLayoutManager(new GridLayoutManager(getContext(), 3));
        shengyouAdapter = new ShengyouAdapter();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerview.setAdapter(shengyouAdapter);

        shengyouAdapter.setEmptyView(getEmptyView());
        shengyouAdapter.setOnItemClickListener(this);
        shengyouAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        shengyouAdapter.isFirstOnly(true);
        shengyouAdapter.setEnableLoadMore(true);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private View getEmptyView() {
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("没有数据");
        return textView;
    }

    @Override
    public void onFirstVisible() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        }, 500);
    }

    private void getData() {
        Api.getInstance().getActressList(type, page, pageSize, new BaofangCallback<ShengyouListResponse>(){
            @Override
            public void onFailure(Call<BaseResponse<ShengyouListResponse>> call, Throwable t) {
                toast("获取数据错误：" + t.getLocalizedMessage());
                if(page != 1){
                    shengyouAdapter.loadMoreFail();
                }
                swipeRefreshLayout.setRefreshing(false);
            }
            @Override
            public void onSuccess(ShengyouListResponse shengyouListResponse) {
                swipeRefreshLayout.setRefreshing(false);
                if(shengyouListResponse.pageNo * pageSize >= shengyouListResponse.total) {
                    shengyouAdapter.loadMoreEnd();
                }
                else {
                    shengyouAdapter.loadMoreComplete();
                    shengyouAdapter.setOnLoadMoreListener(ShengyouListFragment.this, recyclerview);
                }
                if(page == 1){
                    shengyouAdapter.setNewData(shengyouListResponse.data);
                } else {
                    shengyouAdapter.addData(shengyouListResponse.data);
                }
                shengyouAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(!Utils.isLogin()){
            startActivity(LoginRegActivity.class);
            return;
        }
        UserInfo userInfo = ((ShengyouAdapter)adapter).getItem(position);
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        intent.putExtra("userinfo", userInfo);
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getData();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }
}
